#ifndef MD2MODEL_H_
#define MD2MODEL_H_

#include "../stdafx.h"
#include "Model.h"

#define MD2_TEXTURE_COUNT 1
struct Md2Header {
	int id; //IDP2
	int version; 
	int texWidth;
	int texHeight;
	int frameSize;
	int numTextures; //the number of textures
	int numVertices; //the number of vertices
	int numTexCoords; //the number of texture coordinates (u, v)
	int numFaces; //the number of triangles
	int numGlCmd; //the number of GL commands in GlCmd list
	int numFrames; //the number of frames 
	
	/* offset */
	int offTextures; //offset to texture names
	int offTexCoords; //offset to texture coordinates
	int offFace; //offset to triangle list
	int offFrame; //offset to the first animation frame
	int offGlCmd; //offset to GL commands
	int offEof; //offset to End of File (EOF)	
};

struct Md2TextureCoordinate {
	short compressedU; //2 bytes, u = (float) compressedU / texWidth
	short compressedV; //2 bytes, v = (float) compressedV / texHeight
};

struct Md2Face {
	short indexVertex[3]; //index to vertex data
	short indexTexture[3]; //index to texture coordinate data
};

struct Md2FrameHeader {
	float scale[3];
	float translate[3];
	char name[16]; //the name of the frame
};

struct Md2Vertex {
	byte compressedVertex[3]; //vertex (float) = compressedVertex (byte) * scale + translate
	byte normal; //index to a pre-calculated normal vector 
};

struct Md2Frame {
	Md2Vertex* vertices;
};

class Md2Model : public Model
{
public:
	Md2Model();
	virtual ~Md2Model();
	
	void Load(const char* file);
	void LoadTexture(const char* file);
	void NextFrame();
	void Update(long time);
	void Render();
	
private: /* internal model data */
	Md2Header* header;
	Md2TextureCoordinate* textureUV; //a table of texture coordinate
	Md2Face* faces; //a table of faces
	Md2FrameHeader* frameHeader;
	Md2Frame* frames; //vertices are stored separately for each frame
	
	/* decompressed vertices and texture coordinates */
	Vector3f* fVertices; //the whole vertices of all frames
	int curFrame; //current frame;
	int curTimeElapsed;
	int frameTimeElapsed;
	Vector3f* fTextures; //texture coordinates
	
private: //texture management
	GLuint textures[MD2_TEXTURE_COUNT];
private:
	void Cleanup();	
};

#endif /*MD2MODEL_H_*/
